<?php
namespace app\center\controller;
use app\center\logic\DayDataLogic;

/**
 * 每日数据api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/31
 * Time: 14:21
 */
class DayData extends Base {

    public function __construct(){
        parent::__construct();
        $this->dd_logic = new DayDataLogic();
    }

    /**
     * 获取概述信息
     * @return \think\response\Json
     */
    public function ajax_get_overview(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $wrd = new \app\common\controller\WorkRecordData();

        $maps = $this->dd_logic->get_search_wrdt_maps($params['department']);
        $list = model('user')->get_column($maps,'DISTINCT department','','','','','',false);

        $today_shop_person_num = 0;
        $front_money = 0;
        $vip_back_money = 0;
        $check_in_back_money = 0;
        $agency_back_money = 0;
        $account_back_money = 0;
        $trademark_back_money = 0;
        $science_money = 0;
        $other_money = 0;
        $now_money = 0;
        $target_money = 0;
        foreach($list as $k => $v){
            //获取每日数据统计
            $total = $wrd->get_total($v,$params['start_time'],$params['end_time']);
            $today_shop_person_num += $total['today_shop_person_num'];
            $front_money += $total['front_money'];
            $vip_back_money += $total['vip_back_money'];
            $check_in_back_money += $total['check_in_back_money'];
            $agency_back_money += $total['agency_back_money'];
            $account_back_money += $total['account_back_money'];
            $trademark_back_money += $total['trademark_back_money'];
            $science_money += $total['science_money'];
            $other_money += $total['other_money'];
            $now_money += $total['now_money'];
            $target_money += $total['target_money'];
        }

        $data['today_shop_person_num'] = $today_shop_person_num;
        $data['front_money'] = $front_money;
        $data['vip_back_money'] = $vip_back_money;
        $data['check_in_back_money'] = $check_in_back_money;
        $data['agency_back_money'] = $agency_back_money;
        $data['account_back_money'] = $account_back_money;
        $data['trademark_back_money'] = $trademark_back_money;
        $data['science_money'] = $science_money;
        $data['other_money'] = $other_money;
        $data['now_money'] = $now_money;
        $data['target_money'] = $target_money;
        return json($this->dd_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取每日数据
     * @return \think\response\Json
     */
    public function ajax_get_dd(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $wrd = new \app\common\controller\WorkRecordData();

        $maps = $this->dd_logic->get_search_dd_maps($params['department'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_record_data')->get_list($maps,'*','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['front_money'] = deal_number_div_100($v['front_money']);
            $list[$k]['vip_back_money'] = deal_number_div_100($v['vip_back_money']);
            $list[$k]['check_in_back_money'] = deal_number_div_100($v['check_in_back_money']);
            $list[$k]['agency_back_money'] = deal_number_div_100($v['agency_back_money']);
            $list[$k]['account_back_money'] = deal_number_div_100($v['account_back_money']);
            $list[$k]['trademark_back_money'] = deal_number_div_100($v['trademark_back_money']);
            $list[$k]['science_money'] = deal_number_div_100($v['science_money']);
            $list[$k]['other_money'] = deal_number_div_100($v['other_money']);
            $list[$k]['today_total_money'] = deal_number_div_100($v['today_total_money']);
            $list[$k]['pic'] = !empty($v['pic']) ? json_decode($v['pic'],true) : [];

            //获取目前金额
            $now_money = $wrd->get_month_now_money($v['department'],$v['now_date']);
            $list[$k]['now_money'] = $v['today_total_money'] + $now_money;

            //获取目标金额
            $target_money = $wrd->get_month_target_money($v['department'],$v['now_date']);
            $list[$k]['target_money'] = $target_money;

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['name'] = $v['department'] . '-' . $u_detail['username'];
        }

        $data['list'] = $list;
        $data['page'] = $params['page'];
        return json($this->dd_logic->get_response(1,'OK',$data));
    }
}
